package com.dzt.androidframework.view;

import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.dzt.androidframework.R;
import com.dzt.androidframework.activity.ActivityTitle;

import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 转自：https://github.com/AnliaLee/Progressbar
 */
public class CircleBarActivity extends ActivityTitle {

	@ViewInject(R.id.circle_view)
	private CircleBarView circleBarView;
	@ViewInject(R.id.circle_view2)
	private CircleBarView circleBarView2;
	@ViewInject(R.id.circle_view3)
	private CircleBarView circleBarView3;
	@ViewInject(R.id.tv_progress)
	private TextView tvProgress;
	@ViewInject(R.id.circle_view4)
	private CircleBarView circleBarView4;
	@ViewInject(R.id.tv_progress2)
	private TextView tvProgress2;
	@ViewInject(R.id.circleRangeView)
	private CircleRangeView circleRangeView;

	@Override
	protected int getLayoutId() {
		return R.layout.activity_circle_bar;
	}

	@Override
	protected void initParams() {
		setTitleView(R.string.view_2);
		circleBarView.setProgressNum(100, 3000);
		circleBarView2.setProgressNum(100, 5000);
		circleBarView3.setOnAnimationListener(new CircleBarView.OnAnimationListener() {
			@Override
			public String howToChangeText(float interpolatedTime, float updateNum, float maxNum) {
				DecimalFormat decimalFormat = new DecimalFormat("0.00");
				String s = decimalFormat.format(interpolatedTime * updateNum / maxNum * 100) + "%";
				return s;
			}

			@Override
			public void howTiChangeProgressColor(Paint paint, float interpolatedTime, float updateNum, float maxNum) {
				LinearGradientUtil linearGradientUtil = new LinearGradientUtil(Color.YELLOW,Color.RED);
				paint.setColor(linearGradientUtil.getColor(interpolatedTime));
			}
		});
		circleBarView3.setTextView(tvProgress);
		circleBarView3.setProgressNum(100, 3000);

		circleBarView4.setOnAnimationListener(new CircleBarView.OnAnimationListener() {
			@Override
			public String howToChangeText(float interpolatedTime, float updateNum, float maxNum) {
				DecimalFormat decimalFormat = new DecimalFormat("0.00");
				String s = decimalFormat.format(interpolatedTime * updateNum / maxNum * 100) + "%";
				return s;
			}

			@Override
			public void howTiChangeProgressColor(Paint paint, float interpolatedTime, float updateNum, float maxNum) {
				LinearGradientUtil linearGradientUtil = new LinearGradientUtil(Color.YELLOW,Color.RED);
				paint.setColor(linearGradientUtil.getColor(interpolatedTime));
			}
		});
		circleBarView4.setTextView(tvProgress2);
		circleBarView4.setProgressNum(100, 3000);
	}

	private String [] valueArray;
	private Random random;

	@Override
	protected void initData(Bundle savedInstanceState) {
		valueArray=getResources().getStringArray(R.array.circlerangeview_values);
		random=new Random();
	}

	@Event(value = {R.id.circle_view, R.id.circle_view2,
			R.id.circle_view3, R.id.circle_view4, R.id.circleRangeView})
	private void onViewClick(View v) {
		switch (v.getId()) {
			case R.id.circle_view:
				circleBarView.setProgressNum(100, 3000);
				break;
			case R.id.circle_view2:
				circleBarView2.setProgressNum(100, 5000);
				break;
			case R.id.circle_view3:
				circleBarView3.setProgressNum(100, 3000);
				break;
			case R.id.circle_view4:
				circleBarView4.setProgressNum(100, 3000);
				break;
			case R.id.circleRangeView:
				List<String> extras =new ArrayList<>();
				extras.add("收缩压：116");
				extras.add("舒张压：85");

				int i=random.nextInt(valueArray.length);
				circleRangeView.setValueWithAnim(valueArray[i],extras);
				break;
			default:
				break;
		}
	}

	@Override
	protected String[] initPermissions() {
		return new String[0];
	}
}
